<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--导入模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-import-plan">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Excel信息导入</h4>
            </div>
            <div class="modal-body">
                <form id="importForm">
                    <div class="form-group">
                        <label>模版</label>
                        <a class="mal" th:href="@{/file/原材料计划模板.xlsx}">点击下载</a>
                    </div>
                    <div class="form-group">
                        <label>选择Excel文件并上传</label>
                        <input id="import-file" class="btn" type="file" name="file">
                        <p class="help-block">请按模板格式进行导入，信息与模板信息对应。</p>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="btn-import" type="button" class="btn btn-success">
                    <span class="Bold">确定</span>
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--上传附件-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-upload">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">上传附件</h4>
            </div>
            <div class="modal-body">
                <form id="uploadForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label>选择附件上传</label>
                        <input class="btn" type="file" name="file">
                        <p class="help-block">多个文件请自行打包上传，大小不要超过100M</p>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button shiro:hasPermission="report:list:edit" id="btn-upload" type="button"
                        class="btn btn-success">
                    <span class="Bold">上传</span>
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content">
    <div class="row" style="margin-bottom: 24px">
        <a th:href="@{/report/list}"
           class="btn btn-default pull-right" style="margin-right: 10px">
            <span class="Bold">取消</span>
        </a>
        <a id="btn-save" shiro:hasPermission="report:list:edit" href="javascript:void(0);"
           class="btn btn-success pull-right" style="margin-right: 10px">
            <span class="Bold">保存</span>
        </a>
        <!--<a shiro:hasPermission="report:add:view" href="javascript:void(0);"
           class="btn btn-success pull-right" style="margin-right: 10px"
           onclick="js.modal.open('modal-import-plan')">
            <span class="Bold">导入</span>
        </a>-->
    </div>
    <div class="row">
        <form id="sheetForm" class="form-horizontal">
            <div class="col-xs-11 col-xs-offset-1">
                <input type="hidden" name="id" th:value="${sheet.id}">
                <table class="t1 t3">
                    <thead>
                    <tr>
                        <td colspan="12">
                            <img th:src="@{/img/logo3.jpg}" width="64px" height="40px" style="margin-top: -30px">
                            <div style="display: inline-block;margin-top: 5px;margin-left: 20px">
                                <span style="letter-spacing: 24px;display: block;line-height: 20px">&nbsp;辽宁忠旺机械设备制造有限公司</span>
                                <span style="display: block;font-size: 12px;line-height: 20px">Liaoning Zhongwang Machinery Equipment Manufacturing Co., Ltd</span>
                            </div>
                        </td>
                        <td rowspan="2">
                            <div id="qrCode-div" style="display: none"></div>
                            <img id="qr-img" width="80px" height="80px">
                        </td>
                        <td style="width: 8%"></td>
                    </tr>
                    <tr>
                        <td colspan="12" style="font-size: 16px;letter-spacing: 4px;">
                            原材料计划单
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="width: 8%">项目编号</td>
                        <td style="width: 11%;font-size: 14px;">
                            <input id="sheetForm-proCode" type="text" class="form-control" name="proCode"
                                   th:value="${sheet.proCode}" readonly>
                        </td>
                        <td style="width: 8%">项目名称</td>
                        <td colspan="3" style="width: 21%">
                            <input id="sheetForm-proName" type="text" class="form-control" name="proName"
                                   th:value="${sheet.proName}" readonly>
                        </td>
                        <td style="width: 8%">项目数量</td>
                        <td style="width: 6%">
                            <input id="sheetForm-proNum" type="number" class="form-control" name="proNum"
                                   th:value="${sheet.proNum}" readonly>
                        </td>
                        <td style="width: 6%">类型</td>
                        <td style="width: 9%">
                            <select id="sheetForm-malType" class="form-control" name="malType">
                                <option value="">请选择</option>
                                <option th:each="type : ${specials}" th:value="${type.name}"
                                        th:text="${type.name}" th:selected="${type.name == sheet.malType}"></option>
                            </select>
                        </td>
                        <td style="width: 6%">部分</td>
                        <td style="width: 9%">
                            <select id="part" class="form-control" name="part">
                                <option th:each="p : ${parts}" th:value="${p.proPart}"
                                        th:text="|${p.special}-${p.proPart}|" th:selected="${p.proPart == sheet.part}"></option>
                            </select>
                            <!--<input id="sheetForm-part" type="text" class="form-control error" name="part"
                                   th:value="${sheet.part}">-->
                            <input type="hidden" name="special" id="special" th:value="${sheet.special}">
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td style="width: 4%">序号</td>
                        <td colspan="4">物料描述</td>
                        <td style="width: 6%">数量</td>
                        <td style="width: 6%">单位</td>
                        <td>型号/标准号</td>
                        <td colspan="2">材质</td>
                        <td>使用库存</td>
                        <td colspan="2">备注</td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="mal">
                    <tr th:each="plan : ${plans}">
                        <td th:text="${planStat.count}"></td>
                        <td colspan="4">
                            <input type="hidden" name="planId" th:value="${plan.id}">
                            <input type="text" class="form-control" name="malDesc" th:value="${plan.malDesc}" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" step="0.1" class="form-control" name="num" th:value="${T(com.zw.pdm.util.BigDecimalUtils).valueFormat(plan.num)}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" th:value="${plan.unit}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="standard" th:value="${plan.standard}" autocomplete="off">
                        </td>
                        <td colspan="2">
                            <select class="form-control" name="quality" title="">
                                <option th:each="q : ${qualities}" th:value="${q.name}"
                                        th:text="${q.name}" th:selected="${q.name == plan.quality}"></option>
                            </select>
                        </td>
                        <td>
                            <input type="text" class="form-control" name="useStore" th:value="${plan.useStore == 1 ? '是' : '否'}" autocomplete="off">
                            <!--<select class="form-control" name="useStore" title="">
                                <option value="是" th:selected="${plan.useStore == 1}">是</option>
                                <option value="否" th:selected="${plan.useStore == 0}">否</option>
                            </select>-->
                        </td>
                        <td colspan="2">
                            <input type="text" class="form-control" name="remark" th:value="${plan.remark}" autocomplete="off">
                        </td>
                        <td>
<!--                            <span class="mal-add" title="添加"><i class="fa fa-plus text-info"></i> </span>-->
                            <span class="mal-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                            <span class="mal-upload" title="上传附件"><i class="fa fa-upload text-success"></i> </span>
                            <a target="_blank" title="下载附件" th:if="${plan.url != null}" th:href="${plan.url}"><i class="fa fa-download text-primary"></i> </a>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-xs-10 col-xs-offset-1" style="font-size: 14px;line-height: 200%;">
                <div class="col-xs-3" style="padding-left: 0">提报人： [[${session.user.name}]]</div>
                <div class="col-xs-3 bottom form-inline">联系电话：
                    <input class="form-control" style="width:60%;border: none;border-bottom: 1px solid #000;" type="text" name="postTel" th:value="${sheet.postTel}">
                </div>
            </div>
        </form>
    </div>
    <div th:include="include :: tipMal"></div>
</section>
<div th:include="include :: footer"></div>
<script th:src="@{/plugin/SheetJs/xlsx.core.min.js}"></script>
<script th:inline="javascript">

    /** 计算表格序号 */
    function computerNo() {
        $("table tbody tr").each(function (i, e) {
            $(this).children("td:first").text(i + 1);
        });
    }

    let gloDeptName = toUtf8('辽宁忠旺机械设备制造有限公司 ' + [[${sheet.deptName}]] + ' ' + [[${sheet.branchName}]]);

    $("#qrCode-div").qrcode({
        width: 80,
        height: 80,
        text: gloDeptName
    });

    /** 获取部分 */
    function getPart() {
        let code = $("#sheetForm-proCode").val();
        let type = $("#sheetForm-malType").val();
        switch (type) {
            case '机械' :
            case '流体' :
            case '自动化' :
                fetchPart(code, type);
                break;
            case '外观' :
                fetchPart(code, null);
                break;
            case '维修备件' :
            case '设备工具' :
            case '消耗品' :
                $("#part").empty().append(`<option value="整体">整体</option>`);
                break;
            case '':
                $("#part").empty().append(`<option value="">请选择</option>`);
                break;
        }
    }
    function fetchPart(code, type) {
        js.get({
            url: ctx + 'est/add/part/list',
            data: {
                code : code,
                type : type
            },
            success: function (result) {
                $("#part").empty().append(`<option value="">请选择</option>`);
                result.forEach(value => {
                    if (value) {
                        $("#part").append(`<option value="${value.proPart}">${value.special}-${value.proPart}</option>`);
                    }
                })
                /*$("#part").append(`<option value="随机备件">随机备件</option>`);
                $("#part").append(`<option value="备品备件">备品备件</option>`);
                $("#part").append(`<option value="不可预见">不可预见</option>`);*/
            }
        })
    }

    $(function () {

        let c = document.getElementsByTagName('canvas')[0];
        $("#qr-img").attr("src", c.toDataURL("image/png"));

        $("#sheetForm-malType").change(function () {
            getPart();
        })

        $("#part").change(function () {
            let a = $(this).find("option:selected").text();
            if (a.indexOf("-") === -1) {
                $("#special").val('');
            } else {
                $("#special").val(a.split("-")[0]);
            }
        })

        /** 添加一行 */
        /*$("#sheetForm").on('click', '.mal-add', function () {
            $(this).parents("tr").after(`
                    <tr>
                        <td></td>
                        <td colspan="3">
                            <input type="hidden" name="planId">
                            <input type="text" class="form-control" name="malDesc" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" step="0.1" class="form-control" name="num" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" autocomplete="off">
                        </td>
                        <td colspan="3">
                            <input type="text" class="form-control" name="standard" autocomplete="off">
                        </td>
                        <td colspan="2">
                            <input type="text" class="form-control" name="brand" autocomplete="off">
                        </td>
                        <td colspan="2">
                            <input type="text" class="form-control" name="remark" autocomplete="off">
                        </td>
                        <td>
                            <span class="mal-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="mal-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
            `);
            computerNo();
        });*/

        /** 导入 */
        /*$('#btn-import').click(function() {

            let files = $("#import-file")[0].files;
            // let files = e.target.files;
            let fileReader = new FileReader();
            fileReader.onload = function(ev) {
                try {
                    let data = ev.target.result,
                        workbook = XLSX.read(data, {type: 'binary'}), // 以二进制流方式读取得到整份excel表格对象
                        plans = []; // 存储获取到的数据
                    // 遍历每张表读取
                    for (let sheet in workbook.Sheets) {
                        if (workbook.Sheets.hasOwnProperty(sheet)) {
                            plans = plans.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                            break; // 只取第一张表
                        }
                    }
                    if (plans.length === 0) {
                        js.modal.alert("无法读取文件内容");
                    } else {
                        $("#mal").empty();
                        plans.forEach((value, index) => {
                            $("#mal").append(`
                                <tr>
                                    <td></td>
                                    <td colspan="3">
                                        <input type="text" class="form-control" name="malDesc" value="${value['物料描述']}" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="number" step="0.1" class="form-control" name="num" value="${value['数量']}" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" name="unit" value="${value['单位']}"  autocomplete="off">
                                    </td>
                                    <td colspan="3">
                                        <input type="text" class="form-control" name="standard" value="${value['型号/标准号']}"  autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="brand" value="${value['品牌']}"  autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="remark" value="${value['备注']}"  autocomplete="off">
                                    </td>
                                    <td>
                                        <span class="mal-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                        <span class="mal-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                        <span class="mal-import" title="上传附件"><i class="fa fa-upload text-success"></i> </span>
                                    </td>
                                </tr>
                            `)
                        });
                        computerNo();
                        js.modal.hide("modal-import-plan");
                    }
                } catch (e) {
                    js.modal.alert("无法读取文件或文件类型有错误");
                }
            };
            // 以二进制方式打开文件
            fileReader.readAsBinaryString(files[0]);
        });*/

        /** 删除行 */
        $('#sheetForm').on('click', '.mal-del', function () {
            let $this = $(this);
            let id = $(this).parents("tr").find("input[name=planId]").val();
            if (id) {
                js.modal.confirm("确定删除吗？", function () {
                    js.post({
                        url: ctx + "report/list/plan/del",
                        data: {id: id},
                        success: function (result) {
                            if (result.type === web_status.SUCCESS) {
                                js.modal.success(result.msg);
                                $this.parents("tr").remove();
                                /*if ($("table tbody tr").length === 0) {
                                    $("#mal").append(`
                                    <tr>
                                        <td></td>
                                        <td colspan="3">
                                            <input type="hidden" name="planId">
                                            <input type="text" class="form-control" name="malDesc" autocomplete="off">
                                        </td>
                                        <td>
                                            <input type="number" step="0.1" class="form-control" name="num" autocomplete="off">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="unit" autocomplete="off">
                                        </td>
                                        <td colspan="3">
                                            <input type="text" class="form-control" name="standard" autocomplete="off">
                                        </td>
                                        <td colspan="2">
                                            <input type="text" class="form-control" name="brand" autocomplete="off">
                                        </td>
                                        <td colspan="2">
                                            <input type="text" class="form-control" name="remark" autocomplete="off">
                                        </td>
                                        <td>
                                            <span class="mal-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                            <span class="mal-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                        </td>
                                    </tr>
                                `)
                                }*/
                                computerNo();
                            } else {
                                js.modal.warning(result.msg);
                            }
                        }
                    });
                });

            } /*else {
                $this.parents("tr").remove();
                if ($("table tbody tr").length === 0) {
                    $("#mal").append(`
                                <tr>
                                    <td></td>
                                    <td colspan="3">
                                        <input type="hidden" name="planId">
                                        <input type="text" class="form-control" name="malDesc" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="number" step="0.1" class="form-control" name="num" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" name="unit" autocomplete="off">
                                    </td>
                                    <td colspan="3">
                                        <input type="text" class="form-control" name="standard" autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="brand" autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="remark" autocomplete="off">
                                    </td>
                                    <td>
                                        <span class="mal-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                        <span class="mal-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                    </td>
                                </tr>
                            `)
                }
                computerNo();
            }*/

        });

        /** 保存计划单 */
        $("#btn-save").click(function () {
            let $this = $(this);
            let res = js.vd({
                id: 'sheetForm',
                notEmpty: ['proCode', 'malType', 'part', 'malDesc', 'num', 'unit', 'quality', 'postTel']
            })
            if (!res) {
                return;
            }
            $this.attr('disabled', true);
            js.submit({
                formId: "sheetForm",
                url: ctx + 'report/list/mal/edit',
                data: new FormData($("#sheetForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        location.href = ctx + "report/list";
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.removeAttr('disabled');
        });

        /** 打开上传模态框 */
        $("#sheetForm").on('click', '.mal-upload', function () {
            let id = $(this).parents("tr").find("input[name=planId]").val();
            $("#uploadForm").find("input[name=id]").val(id);
            js.modal.open('modal-upload');
        });
        /** 初始化验证 */
        js.validate.init("uploadForm", {
            fields: {
                file: {
                    validators: {
                        notEmpty: {message: '上传文件不能为空'}
                    }
                }
            }
        });
        /** 上传附件 */
        $("#btn-upload").click(function () {
            var $this = $(this);
            $this.attr('disabled', true);
            js.validSubmit({
                formId: "uploadForm",
                url: ctx + "report/list/plan/upload",
                data: new FormData($("#uploadForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.modal.hide("modal-upload");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.attr('disabled', false);
        });
        /** 重置表单及验证 */
        js.modal.hideEvent("modal-upload", function () {
            js.validate.reset("uploadForm");
            js.reset("uploadForm");
        })

    });
</script>
</body>
</html>
